﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;

namespace School.Pages.Teaching
{
    public partial class TE_Question_Bank : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1503) == true)
                {

                    if (!this.Page.IsPostBack)
                    {
                        FillControls();
                        ddlBind(ddltype, School.Teaching.Teaching.Get_Question_Bank_Type());
                        ddlBind(ddlcategories, School.Teaching.Teaching.Get_Question_Categories());
                    }
                    lbltst.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }

        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }

        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
            }
        }

        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedIndex > 0)
            {
                ddlBind(ddlsubject, School.Teaching.Teaching.Get_Subject_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue)));
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool re = School.Teaching.Teaching.Add_Bank_Question(Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), Convert.ToInt32(ddlStadge.SelectedValue.ToString()), Convert.ToInt32(ddlGrade.SelectedValue.ToString()), Convert.ToInt32(ddlsubject.SelectedValue.ToString()), txtname.Text, Convert.ToInt32(ddlcategories.SelectedValue.ToString()), Convert.ToInt32(ddltype.SelectedValue.ToString()));
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تمت الاضافة بنجاح";
                txtname.Text = "";
                if (ddlsubject.SelectedIndex > 0)
                    gvBank_QBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        private void gvBank_QBind()
        {
            gvBank_Q.DataSource = School.Teaching.Teaching.Get_Bank_Question_BySubj(Convert.ToInt32(ddlsubject.SelectedValue));
            gvBank_Q.DataBind();
        }

        protected void gvBank_Q_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBank_Q.PageIndex = e.NewPageIndex;
            gvBank_QBind();
        }

        protected void gvBank_Q_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvBank_Q.EditIndex = -1;
            gvBank_QBind();
        }

        protected void gvBank_Q_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvBank_Q.EditIndex = e.NewEditIndex;
            gvBank_QBind();
        }

        protected void gvBank_Q_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvBank_Q.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Teaching.Teaching.DeleteBank_Question(Id);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم الحذف بنجاح";
                gvBank_QBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvBank_Q_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }

        protected void gvBank_Q_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvBank_Q.DataKeys[e.RowIndex].Value.ToString());
            TextBox txtQues = (TextBox)gvBank_Q.Rows[e.RowIndex].FindControl("txtQues");


            bool re = School.Teaching.Teaching.UpdateBank_Question(Id, txtQues.Text);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
                gvBank_Q.EditIndex = -1;
                gvBank_QBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void ddlsubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlsubject.SelectedIndex > 0)
                gvBank_QBind();
        }
    }
}
//txtQues